You know things are bad on the boat you’re on when climbing on board a pirate ship seems like a good solution. I mean, pirates are not known for their fair and just treatment of stowaways. Or guys with swords who got on their ship to fight them? We don’t really know the circumstances.
But we do know that Hamlet is on a trip to England he doesn’t want to make with his “friends” who he knows are delivering his death warrant. There aren’t a whole lot of ways out of this situation. Jumping on a pirate ship seems rather reasonable in that case.
However, I find I am suspicious of these pirates who just happen to turn up and just happen to take only Hamlet prisoner. I mean. Hamlet’s crafty. He could have sent a message to the pirates that he’d give them a hefty bounty if they attacked his ship. I wouldn’t put it past him to set up this fortunate circumstance.