The theory is that sailors, away from wives and loved ones would create a surrogate relationship with their nearest means of home, comfort and safety - the ship, and lavish emotional affection in 'her' instead.
Same reason that cars are she, or the sea. They are beautiful, headstrong, willfull, occasionally biddable and you cant cope without them. Remind you af any gender in particular?