There is lots of demand for housing relative to supply of housing.
There is lack of supply of cities with equivalent income opportunities and amenities of SF relative to demand for that type of city.
All various ways of discussing the supply and demand curves that cause prices. And multiple solutions to address each one, assuming the goal is to make prices go lower. Such as increasing income/wealth tax, vacancy tax, increasing property taxes, increasing supply of housing, increasing cities like SF with labor laws similar to CA, etc.
And buying a unit to rent it does not change the housing supply. It is new people moving to a city that fills up supply. Is that what you meant by profitability: buying an apartment to rent? Every city has landlords.
demand for investing in the limited supply of apartments based on their profitability is not the same thing as demand for the actual product of housing, which is what supply and demand in the housing market is supposed to be about - you're basically proving the point that incentives here are perverse