you don't need to sell, just rent out the apartment when you move.
With the rental income, you can pay for a similarly priced location in the new place. Then, when the market fits, make the sale, and you've lost very little (if anything), except upkeep costs due to rental damage, etc.
Even in a landlord friendly state it can take 2-3 months to evict someone for non payment. In some states I've heard that it can take a year.
I've been a landlord, never again.
I'd guess that one of the reasons home prices in 'nice' neighborhoods are so high (or HOAs charge high dues) is a sort of signaling, similar to nuptial gifts in animals, that you have your act together and are invested in being a good neighbor.
But as far as buying in a high priced area or even renting in a high priced area, price does provide a filter.
Owning things adds friction one way or another.
The real calculation is the total monthly cost of ownership plus any sale prices on the buying and selling ends over the period, relative to the total costs of renting.
Over the 10 years we've owned our home, yes, it's been worth it to buy, because we can recoup money that would have gone to someone else.
However, over a short period, the sales costs would dwarf any returns we would get.
The sales costs are largely fixed on both ends, as a percent of home value, and diminish as a total percent of gains over the period of ownership. So the period of ownership is relevant.
This in turn is relevant because your mobility becomes relevant.
The previous market we were in too, was so overpriced relative to the rental cost that we actually saved money over the period by renting rather than buying. Then the Great Recession happened.
Either strategy makes sense depending on your mobility risk and the market. I don't think it's clear that one or the other is generally better.