This use of the word "for" is unusual, but it is correct.
It is rare for X
to do Y
Here are some simpler examples that show the usage of "for" in this way:
✔️ Yes: It is rare for Twitter to ban specific advertisers.
-- Jim Finkle, David Ingram, Reuters News Service, 2018
✔️ Yes: It is rare for a player to combine the skills of power and speed.
-- Baseball Hall of Fame (on Facebook)
✔️ Yes: It is a rare instance for a player to combine the skills of power and speed.
-- Kevin Stiner, "Ott’s first career home run an inside-the-park shot", Baseball Hall of Fame
✔️ Yes: It is rare for a ballet's storyline to be overshadowed by science. So it is with “Anastasia”
-- The Economist, on Twitter
✔️ Yes: ..it is rare for a government entity to directly criticize a sitting member of Congress.
-- Julie Hirschfeld Davis, The New York Times
This is from a news headline, so it uses abbreviated "headline-speak":
✔️ Yes: Minnesota man catches a mystery fish; Rare for its color, remarkable for its age
-- KSTP-TV, Minnesota, USA
They are saying "the fish is rare {for its color}". Its color is unusual, so that makes the fish a rare fish.
The color makes the fish rare.
In Your Example
Your example was:
The rally marked a rare visit for the president to {a state won by Democratic contender Hillary Clinton during the 2016 presidential race}.
This can be reduced to:
The rally marked a rare visit for the president to {New Hampshire
}.
This can be simplified to:
{This was
} a rare visit for the president to New Hampshire.
which is a complicated way of saying:
It is rare for the president to visit New Hampshire.