Humza Yousaf is on course to become the first Muslim to lead a democratic western European nation, and when that happens, he will confront several daunting obstacles.The pro-independence Scottish National Party on Monday elected Humza Yousaf, the country’s health secretary, as its top official, putting the 37-year-old minister on track to become the first Muslim to lead a democratic western European nation.Mr. Yousaf emerged with a narrow victory in a bruising leadership race that followed the surprise resignation last month of Nicola Sturgeon, who had dominated Scottish politics for almost a decade as the country’s first minister and leader of the S.N.P.In choosing Mr. Yousaf, members of his party opted for the candidate thought most likely to stick with Ms. Sturgeon’s progressive agenda, rejecting a more socially conservative contender, Kate Forbes.“We will be the generation that delivers independence for Scotland,” said Mr. Yousaf after the result was announced, and before a vote on Tuesday in the Scottish Parliament to confirm him as the country’s first minister.As the new leader of the S.N.P. — the largest party in Scotland’s Parliament — that should be a formality. But, referring to some of the wider problems he faces, Mr. Yousaf appealed for unity after a divisive leadership contest that fractured a party previously renowned for its discipline.“Where there are divisions to heal we must do so and do so quickly because we have a job to do, and as a party we are at our strongest when we are united,” he said.In a sometimes emotional victory speech, Mr. Yousaf thanked his family, including his deceased grandparents, who emigrated to Scotland.“I am forever thankful that my grandparents made the trip from the Punjab to Scotland over 60 years ago,” he told the audience at Murrayfield, Scotland’s national rugby stadium, where the leadership results were announced. “As immigrants to this country, who knew barely a word of English, they could not have imagined their grandson would one day be on the cusp of being the next first minister of Scotland.”Sunder Katwala, director of British Future, a research institute that focuses on identity issues, described Mr. Yousaf as “the first Muslim to be elected as a national leader in any western democracy,” writing that it was “a breakthrough moment that should resonate well beyond Scotland.”That in part reflects a growing diversity in the higher reaches of British politics. Anas Sarwar, leader of the Scottish opposition Labour Party, is also Muslim, while Britain’s prime minister Rishi Sunak, follows the Hindu faith.Though Mr. Yousaf was on top after the first ballot, he failed to win more than half of the votes cast by party members in the initial round of voting, as required to win the race. But once the third-place candidate, Ash Regan, was eliminated and her votes were redistributed, Mr. Yousaf won 52.1 percent, to 47.9 percent for Ms. Forbes.Scotland’s outgoing first minister, Nicola Sturgeon, center, at an heatlh center in Fife. She had dominated Scottish politics for almost a decadePeter Summers/Agence France-Presse — Getty ImagesHaving served as transport minister, justice secretary and health secretary, Mr. Yousaf was seen as the preferred candidate of the party’s establishment, but his record in government was questioned by his opponents.“You were transport minister and the trains were never on time, when you were justice secretary the police were stretched to breaking point, and now as health minister we’ve got record high waiting times,” said Ms. Forbes, his main challenger, during a televised leadership debate.The social conservatism and strong religious beliefs of Ms. Forbes, who was on maternity leave from her position of finance secretary when Ms. Sturgeon quit, featured prominently in the leadership contest.A member of the evangelical Free Church of Scotland, Ms. Forbes said she would have voted against single-sex marriage had she been in the Scottish Parliament when it was approved in 2014, and that she believed that having children outside of marriage is “wrong” according to her faith.Another social question — gender recognition — became a political battleground just before Ms. Sturgeon’s resignation, when Britain’s government rejected legislation from Scotland’s Parliament making it easier for people to change their gender. Mr. Yousaf said on Monday that he would seek to challenge the British government’s decision.Had Ms. Forbes been elected, the Scottish Greens might have withdrawn their support for the S.N.P.-led government in Edinburgh, reducing it to a minority administration.The new leader faces numerous challenges both in replacing Ms. Sturgeon, who was a popular leader and skilled communicator, and in charting a course to independence.Ms. Sturgeon took over the leadership after Scots voted by 55 percent to 45 percent against independence in a referendum in 2014. Since then, sentiment on the issue has not shifted significantly.Ms. Sturgeon’s resignation came after the British Supreme Court ruled that a second referendum could not be held without the agreement of Britain’s government in London, which opposes such a move. Mr. Yousaf’s task will be to try build support for independence to such a level — perhaps around 60 percent in opinion polls — that it would be politically impossible for London to ignore calls for another vote.His leadership victory also has implications for the rest of Britain, where a general election must take place by January 2025. If the result is close, the S.N.P.’s performance could play a decisive role in determining the next prime minister.Given the divisions within the S.N.P. and the difficulties replacing Ms. Sturgeon, Britain’s main opposition Labour Party, which once dominated Scottish politics but has seen its influence dwindle as the S.N.P. gathered strength, now senses an opportunity to claw back some of its old seats in Scotland. More