Is it possible to divert a plane to a distant airport?
The diversion airport is selected by the captain¹.

If the aircraft needs to divert for some urgent reason like engine failure, smoke/suspected fire/fire, medical emergency and similar, the captain will select the airport so that they can land as soon as possible. However from cruise altitude even expedient descent still takes about 15–30 minutes and the plane can cover 50–100 nmi, so they still have some choice in most places, especially densely populated areas like Europe.

But there are many less urgent reasons that warrant diversion. The most common is of course that the intended destination is closed due to weather, accident or other problem. And then there are problems like pressurization failure that require descent to lower altitude where the fuel consumption is higher, so the plane no longer has enough fuel to reach the original destination, but still has fuel to fly considerable distance.

Sometimes the plane can even reach the intended destination just fine, but some fault occurs that would be hard to fix there, so the plane returns or diverts somewhere it will be easier to fix.

In these cases, the captain calls the airline dispatching (over radio, HF radio or satellite phone) and discusses with them where they should divert to make least disruption to the overall airline operations considering things like ability to fix the diverting plane, availability of replacement plane or alternate connections for the passengers, availability of replacement crew to fly the replacement plane or this plane after it is fixed, availability of airline representatives to take care of the passengers and many other things.

So something happened and for some operational reason it was easier for the airline if they had the plane in Krakow than if they had it in Gdansk (or, if it was not possible to have it in Gdansk, better to have it in Krakow than, say, Warsaw, which was still closer). For example I remember reading about incident where wind shield cracked on a plane already on approach to their destination, Klagenfurt, and they returned to Vienna and sent the passengers by bus. Because it was apparently easier (and cheaper) for the airline than fixing the airplane in Klagenfurt without suitable hangar (with cracked winshield the plane may complete the flight, but not take off again).



¹ Except really special circumstances like violation of foreign airspace and arrest by interceptors, nobody from outside can divert the plane. The pilots do.