One answer that I had heard was that it's been standard practice for various municipalities to buy vagrants one-way tickets to SF. (Don't know if it's true or apocryphal.)
(edit: TIL it is called "greyhound therapy" as described in brandur's comment: https://news.ycombinator.com/item?id=6830558)
That, combined with the mostly hospitable climate for homelessness, and the municipal tolerance for ... unconventional ... lifestyles, would explain at least part of it.
Also, it's where the 60s "happened", so at least some of the head cases can be attributed to taking too many strange drugs.