If I remember correctly, I believe that this is secretly Parseval's Identity. What you have is that the sets $\\{ |k\rangle \\}_k$ and $\\{\langle k|\\}_k$ are orthonormal bases for the $L^2$ Hilbert space (and its dual) on which $H_1$ and $H_2$ are acting. In this case, Parseval's Identity roughly states that the identity $I$ can be written as $$ I = \sum_k |k\rangle \langle k | $$ You can then use this in calculations as $$ \langle \varphi | = \langle \varphi | I = \sum_k \langle \varphi | k \rangle \langle k | $$ and analogously $$ |\phi\rangle = I|\phi\rangle = \sum_{k} |k\rangle \langle k|\phi\rangle. $$ In particular, $$ \langle \varphi | \phi \rangle = \sum_k \langle \varphi | k \rangle \langle k | \phi \rangle $$