Any kind of fossil fuel generation means constantly going out and digging up new oil sources, shipping them around the world, and then burning them. So you invest a lot of time & money into something that disappears immediately and also heats up the environment.
Meanwhile, a solar panel just sits there for decades passively making energy with very few externalities.
Not to mention, recycling solar panels & batteries is getting cheaper & more effective by the day. The metal (and even oil!) you dug out of the ground to build them didn’t get burned up; a lot of it is still usable.