Multiplying a matrix with a scalar - dimensional consistency Let us represent an $n\times 1$ vector as $\vert .\rangle$ and a $1\times n$ vector as $\langle . \vert$. Consider the following expression where $X$ and $Y$ are $n\times n$ matrices. 

$$E(X) = \langle i \vert X\vert i\rangle Y$$

Suppose $Y$ can be written as a product of two more $n\times n$ matrices $A$ and $B$. Then

$$E(X) = \langle i \vert X\vert i\rangle AB = A\langle i \vert X\vert i\rangle B,$$

where the change of order is allowed since $\langle i \vert X\vert i\rangle$ is just a number. I'm not sure how to think about the terms $A\langle i \vert$ and $\vert i\rangle B$. They seem dimensionally inconsistent, yet they should work out to be an $n\times n$ matrix. What is the issue here?
$A\langle i\vert$ is not an $N\times N$ matrix.  It is an $N\times N^2$ matrix.  More precisely, you should think of this (and correspondingly the other products in your question) as a tensor product of an $N\times N$ matrix with a $1\times N$ vector, that is,
$$ A\otimes \langle i\vert\ .$$
Notice that $\langle i |\cdot X\cdot |i\rangle$ is not actually “just a number,” but really a $1\times 1$ matrix (because the product of two matrices is always a matrix). Since $\langle i|X|i\rangle$ is not a scalar, the product $\langle i|X|i\rangle Y$ is not well-defined, since $i$ is $1\times 1$ and $Y$ is $n\times n$.