William married Mary because she was close to the throne but also because Charles II wanted to ensure that there was a Protestant succession (or at least defuse the mistrust engendered by James's Catholicism. Marrying his niece to a staunchly Protestant prince did that.