Algebra and Substitution in Quadratic Form―Einstein Summation Notation 
Schaum's Outline to Tensor Calculus ― chapter 1, example 1.5 ―――
If $y_i = a_{ij}x_j$, express the quadratic form $Q = g_{ij}y_iy_j$ in terms of the $x$-variables.
Solution: I can't substitute $y_i$ directly because it contains $j$ and there's already a $j$ in the given quadratic form. So $y_i = a_{i \huge{j}}x_{\huge{j}} = a_{i \huge{r}}x_{\huge{r}}$.
This implies $ y_{\huge{j}} = a_{{\huge{j}}r}x_r.$ But I already used $r$ (in the sentence before the previous) so need to replace $r$ ――― $ y_j = a_{j \huge{r}}x_{\huge{r}} =  a_{j \huge{s}}x_{\huge{s}}.$
Therefore, by substitution, $Q = g_{ij}(a_{ir}x_r)(a_{js}x_s)$
$$ = g_{ij}a_{ir}a_{js}x_rx_s. \tag{1}$$ $$= h_{rs}x_rx_s, \text{ where } h_{rs} = g_{ij}a_{ir}a_{js}.   \tag{2}$$

Equation ($1$): Why can they commute $a_{js}$ and $x_r$? How are any of the terms commutative?
Equation ($2$): How does $rs$ get to be the subscript of $h$? Why did they define $h_{rs}$?
 A: It's easiest to see what's going on if you put the implied summation symbols back in.  Right before your equations (1) and (2), you have:
$$Q = \sum_{i,j} g_{ij} \sum_r a_{ir} x_r \sum_s a_{js} x_s$$
Now pull all the summations out to the front.  Then all the terms in the summation symbol are numbers, so they commute, and we get $$Q= \sum_{i,j} \sum_{r} \sum_s g_{ij} a_{ir}x_r a_{js}x_s = \sum_{i,j,r,s} g_{ij} a_{ir} \color{green}{x_r a_{js}}x_s = \sum_{i,j,r,s} g_{ij} a_{ir} \color{green}{a_{js} x_r} x_s.$$
This is your equation (1).  Now bring the $r$ and $s$ summations back in: $$Q = \sum_{i,j} g_{ij} a_{ir} a_{js} \sum_{r,s} x_r x_s.$$
This is your equation (2). 
A: The name of repeated indices is non important; in  expressions like
$$y_i=a_{ij}x_j$$
you can change $j$ to any other index different from $i$, as it is repeated. In other words
$$y_i=a_{ij}x_j=a_{ik}x_k=a_{il}x_l=\dots$$
are all represeantations of the same object, i.e. $y_i$.
Let us consider the quadratic form $Q=g_{ij}y_iy_j$. It is defined using the indices $i$ and $j$. In the definitions of $y_i$ and $y_j$ we must choose other repeated indices, to avoid confusion with the sum over $i$ and $j$. Let us do it by choosing $k$ and $l$ s.t.
$$y_i=a_{ik}x_k, $$
$$y_j=a_{jl}x_l. $$
We arrive at the expression
$$Q=g_{ij}y_iy_j=Q=g_{ij}a_{ik}x_ka_{jl}x_l=(a_{\bullet\bullet}\text{ are just scalars!Scalars commute})=g_{ij}a_{ik}a_{jl}x_kx_l,$$
or
$$Q=\sum_{i,j,k,l}g_{ij}a_{ik}a_{jl}x_kx_l, $$
which is quadratic in the $x_{\bullet}$'s, as claimed. I hope this helps with the first question of yours.
In your second question you introduce the quantity $h$ through the components
$$h_{rs}:=g_{ij}a_{ir}a_{js}.$$
The question is: why is the definition of $h_{rs}$ well posed? Let us have a better look at the definition of the $h_{rs}$; the $h_{rs}$ are equivalent to
$$h_{rs}=\sum_{i,j}g_{ij}a_{ir}a_{js},$$
as the repeated indices are $i$ and $j$. This is really the definition of the Einstein notation, no hidden trick here.
The free indices in 
$$g_{ij}a_{ir}a_{js} $$ 
are $r$ and $s$. The quantity "$h$" is then defined for each pair of $(r,s)$. In other words, it is a matrix whose components are just the $h_{rs}$'s. I hope it helps.
A: In equation (1) $a_{js}$ and $x_r$ commute because these are just regular (reals or complex) numbers using standard multiplication which is commutative.
Equation (2) $h_{rs}$ is defined to save space more than anything, it's the coefficients of the polynomial in $x_i$.
