what is the grammatical name and function of this expression '...why he had to marry again...'

The expression '...why he had to marry again...' is an interrogative clause. Its function is to ask a question.

The grammatical name of the expression "why he had to marry again" is a noun clause functioning as a direct object.

The function of this noun clause is to provide additional information about the verb "had" and answer the question "why." It introduces a subordinate clause and serves as the object of the verb.