Tesla and other Musk companies make almost all location decisions based primarily on attracting the right people to join the team. The only notable exception was Boca Chica being chosen for Starbase.
Palo Alto and more broadly Silicon Valley is a very special place for finding the type of brilliant and motivated engineers Tesla is looking for. Despite the rise of other tech hubs like Seattle, Boston and Austin, there still is really no equivalent substitute anywhere else in the world. Silicon Valley has dominated the global high tech industry since the 1950s before even the invention of the integrated circuit. Stanford University and the rest of the ecosystem around it isn’t going to pick up and move any time soon, nor Tesla’s Fremont factory. Unless something radically changes, Tesla will continue to expand in the Bay Area for years to come.