In general it is possible to do that (assuming the phone has an unlockable bootloader), but you can't do that for the exact same image from the manufacturer, because they don't provide the source for a lot of it.
You might be able to install a third-party, community-maintained OS, like LineageOS, though, if your hardware is supported. The downside is that I believe apps like Google Pay won't work anymore, since they require Google's SafetyNet attestation system to pass. Sometimes there are ways around that, but they always seemed like unreliable hacks to me.
Didn't use to be that way. Android phones with Qualcomm SoCs used to be incredibly easy to modify with alternative versions of Android. That changed around 2014-15, can't remember why.
Now it seems only Google's own Pixel phone is the only one that's hackable enough to run LineageOS or /e/ or other de-Googled distros.
LineageOS has active support for 200 devices, and many more if you're up for some hacking, and download the ROM from the XDA forums. /e/ sells phones preinstalled with /e/OS, Pixels, Samsungs, and the Fairphone [0]. CalyxOS can also go on a bunch of phones.
That works on x86 PC platform because boot process and hardware discovery are well standardized, and Kernel is overwritable while the OS is running. Those are not guaranteed on non-PC computers.
You might be able to install a third-party, community-maintained OS, like LineageOS, though, if your hardware is supported. The downside is that I believe apps like Google Pay won't work anymore, since they require Google's SafetyNet attestation system to pass. Sometimes there are ways around that, but they always seemed like unreliable hacks to me.