The sums involved are indeed eyewatering, though not so large that money couldn't be found if it were a government priority to do so - a few tens of billions over a few decades. But the economics of doing it work out, as multiple studies over the last 80 years or so have found, with a return on investment generally around the 7% mark. Not brilliant, but not too shabby either.
As a very rough rule of thumb, if a line sees more than about 20 carriages per hour (between both directions), it's almost certainly worth electrifying; if it sees between 10 and 20, it's worth looking in to. If it sees less than 10, it probably isn't going to be worthwhile.