Look up e.g. the Vacancy Report by the ACCE institute, which states: "With more than 36,000 unhoused residents, Los Angeles simultaneously has over 93,000 units sitting vacant, nearly half of which are withheld from the housing market."
"Why would investors want to lose money on an empty unit when they can simply rent out the unit instead?"
Properties are often used to protect wealth from recession and inflation. Depending on local laws it might be considered too costly to have e.g. low-paying tenants as the value of a property is affected by occupants. A vacancy tax is often named as a way to force the property owners to have the property be of use to the public.