Welcome to TRS
Yes, you can do it its just not a good swap to do
Changing engine size requires the same work regardless of the engine you chose, engine/trans, computer and wiring harness
Using an explorer or ranger engine/trans makes it a bit easier since parts fit better
But you can go out and buy a factory 4.0l Ranger, and usually cheaper than cost of the swap, and better since its factory built
The V8 swap was popular because you can NOT buy a Factory V8 Ranger
IMO you would be better off selling 3.0l Ranger, as is or with repaired 3.0l engine, and then buying a 4.0l Ranger maybe with a bad engine, and swapping in the explorer 4.0l.
In 1997 Explorer there were TWO different 4.0l, OHV and SOHC
SOHC had timing chain issues so would need that repair to be used in any case
OHV has less power than SOHC but was a more reliable engine IMO