I have never "replaced" an engine. (meaning exchanged one for another)
I have pulled them, rebuilt them, and then reinstalled the same engine.
Obviously, some damage to engines makes replacement a requirement (rod thru block; massive cylinder scoring when over-bore can't fix; etc).
But generally I like to install the same engine, as all the accessories are assured to bolt up, etc.
It can be problematic especially when getting from a model year that's "close" to the one you need, but can have many minor differences when it comes to electronic sensors, etc. That can make it a real challenge.