Rashi teaches that "I shall be sanctified" refers to the mitzvah of martyrdom, dying in sanctification of G-d's Name. He adds: "When undergoing martyrdom, have the intention to die. Because those who hope for a miracle (to save them from death), will not be granted a miracle.
Rashi doesn't usually discuss the specifics of a mitzvah, unless this sheds light on the word usage, grammar, or syntax of the Text. Why does he do so here? Also, of the many laws about martyrdom, why did Rashi choose this particular law, about having the correct intention?
The Talmud (Ta'anis, 11b) tells us that we should always remember that there is holiness within us. Every Jew is a manifestation of G-d's Holiness. Every Jew has within him/herself "an aspect of G-d Above" (Iyov,31:2 ). Indeed, this is the intention in the conclusion of our verse: "I am the L-rd, Who has sanctified you."
Martyrdom encompasses more than just dying. "I shall be sanctified among the Children of Israel" is a call to reveal this latent holiness - this higher essence of ourselves - which is concealed by the material nature of this world. To die in sanctification of G-d's Name, to give up our life, means to give up our selfishness, our egotism, etc. - to disregard our instinct to survive, and reveal our holiness. Martyrdom is, mainly, the 'slaying' of our desires, selfishness, egotism, etc., which holds back such acts of self-sacrifice. With that 'slaying' - our holiness becomes revealed, and His Name is sanctified.
Rashi is not recording a specific law among the laws of martyrdom, but defining the mitzvah of sanctifying G-d's Name. This mitzvah consists of being prepared to die the death of a martyr, to live a life in which we constantly slay ('martyr') all self-serving desires.
Expecting a miracle to save us when faced with a martyr's death is a self-serving intention - even if we believe that such a miracle would be a great kiddush haShem. A miracle may well occur, but it is not for us to decide what is best; such decisions originate in our own egos. The only pure intention is subjugation - effacing our will, ourselves, before the Will of G-d. Only then, will the holiness within us be revealed. (Sfas Emes, Emor 5652)
Some people use the phrase kiddush haShem to mean making a favorable impression. But kiddush haShem is really any act which reveals our inner, G-dly holiness. From the kiddush haShem that we can practice, every minute of the day, to the kiddush haShem of death by martyrdom - the underlying factor is: total self-effacement, total obedience to G-d's Will.
Rav Yosef Karo, author of the Shulchan Auruch, was informed by the Maggid Maishorim (a spiritual being), that he would die as a martyr. But, Rav Yosef Karo outlived all his contemporaries and died of natural causes. Chassidim explain the intention of the Maggid Maishorim was as explained above - Rav Yosef Karo was to merit attainment of a high degree of total self-effacement. Indeed, while the names of other famous authors are, somehow, connected to their work (e.g. the "mapah" is refered to as the "ra'mah" an acconym for Rav Moshe Isserlis, etc.), the opinions of Rav Karo are refered to, simply, as being the "Ha'm'chaber"'s - the author's.