Given the choice between being homeless and living in favelas, millions in Brazil have chosen to live in favelas.
The reality of zoning laws in Western countries is to provide a target for regulatory capture by the NIMBY crowd. With the result that we're systemically underbuilding housing, then wonder why we wound up with homelessness.
Favelas are a local optimum which systemically is very difficult to get out of and is a sign that the regulator is powerless. It's a great example of a market failure.